package com.jiwei.community.controller;

import com.jiwei.community.constants.SystemConstant;
import com.jiwei.community.dto.PublishCommentDto;
import com.jiwei.community.entity.Comment;
import com.jiwei.community.entity.Post;
import com.jiwei.community.entity.User;
import com.jiwei.community.enums.AppHttpCodeEnum;
import com.jiwei.community.exception.SystemException;
import com.jiwei.community.service.CommentService;
import com.jiwei.community.service.PostService;
import com.jiwei.community.service.UserService;
import com.jiwei.community.utils.SecurityUtils;
import com.jiwei.community.vo.CommentVo;
import com.jiwei.community.vo.PageListVo;
import com.jiwei.community.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;

/**
 * @author 季伟
 * @date 2024/4/1
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    CommentService commentService;
    @Autowired
    PostService postService;
    @Autowired
    UserService userService;
    @PostMapping("")
    public ResponseResult publishComment(@RequestBody Comment comment){
        Long userId = SecurityUtils.getUserId();
        User user = userService.getById(userId);
        if (user.getStatus().equals(1)){
            throw new SystemException(AppHttpCodeEnum.FAILURE,"用户已被封禁");
        }
        if (StringUtils.isEmpty(comment.getContent()))return ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"content不能为空");
        if (comment.getFromId() == null)return ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"fromId不能为空");
        if (comment.getToCommentId() == null) return  ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"回复id不能为空");
        if (comment.getFromType() == null)return  ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"fromType不能为空");
        return commentService.publishComment(comment);
    }
    @GetMapping("/list")
    public ResponseResult getCommentTreeList(Integer fromType,Long fromId,Integer pageNum,Integer pageSize){
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 10;
        if (fromType.equals(SystemConstant.COMMENT_TYPE_POST)){
            Post post = postService.getById(fromId);
            if (post == null){
                PageListVo<CommentVo> pageVo = new PageListVo<CommentVo>(0L,new LinkedList<CommentVo>());
                return ResponseResult.okResult(pageVo);

            }
        }
        return commentService.getCommentTreeList(fromType,fromId,pageNum,pageSize);
    }



}
